package massGP;

import firing.FiringTree;
import gp.HarbingerOfHeuristics;
import gp.RouletteWheelSelection;
import gp.SelectionStrategy;
import gp.TreeGenerator;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;

public class IterativeMain {
    public static final int POPULATION_SIZE = 100;
    public static final int GENERATIONS = 2;
    public static void main(String[] args) {
	List<FiringTree> population = new ArrayList<FiringTree>();
	SelectionStrategy roulette = new RouletteWheelSelection();
	for(int i=0;i<POPULATION_SIZE;i++) {
	    population.add(TreeGenerator.generateTree());
	    System.out.println("Generated tree " + i + " for first generation");
	}
	for(int i=0;i<GENERATIONS;i++) {
	    HarbingerOfHeuristics.getHeuristic(population);
	    List<FiringTree> temp = population;
	    System.out.println("Generated herustics for generation " + i);
	    population = roulette.getNextGen(temp);
	    System.out.println("spawned generation " + (i+1));
	}
	HarbingerOfHeuristics.getHeuristic(population);
	saveTrees(population);
	System.out.println("Final generation has been saved");
    }
    public static void saveTrees(List<FiringTree> trees) {
	final JFileChooser fc = new JFileChooser();
	fc.setCurrentDirectory(new File("MassTrees"));
	for(int i=0;i<trees.size();i++) {
	    try {
		FileOutputStream fileOut = new FileOutputStream(new File("MassTrees/"+i + "." + Float.toString(trees.get(i).getHeuristic()) + ".ftree"));
		ObjectOutputStream out = new ObjectOutputStream(fileOut);
		out.writeObject(trees.get(i));
		out.close();
		fileOut.close();
	    } catch(IOException e) {
		e.printStackTrace();
	    }
	}
    }
}
